#include "../source/server.hpp"


//管理所有的连接
std::unordered_map<uint64_t, PtrConnection> _conns;
uint64_t conn_id = 0;
// std::vector<LoopThread> threads(2);
// int next_loop=0;
EventLoop base_loop;
LoopThreadPool* loops;



void ConnectionDestroy(const PtrConnection &conn) {
    _conns.erase(conn->Id());
} 
void OnConnected(const PtrConnection &conn) {
    DBG_LOG("NEW CONNECTION:%p", conn.get());
}
void OnMessage(const PtrConnection &conn, Buffer *buf) {
    DBG_LOG("%s", buf->ReadPosition());
    buf->MoveReadOffset(buf->ReadAbleSize());
    std::string str = "Hello World";
    conn->Send(str.c_str(), str.size());
    // conn->Shutdown();
}
// void Acceptor(EventLoop* loop,Channel* lst_channel){
//     int fd=lst_channel->Fd(); 
//     int newfd=accept(fd,nullptr,nullptr);
//     // DBG_LOG("获得了一个fd：%d",fd);
//     if(newfd<0) return;
//     conn_id++;
//     // Channel*channel=new Channel(loop,newfd);
//     PtrConnection conn(new Connection(loop, conn_id, newfd));
//     conn->SetMessageCallback(std::bind(OnMessage, std::placeholders::_1, std::placeholders::_2));
//     conn->SetSrvClosedCallback(std::bind(ConnectionDestroy, std::placeholders::_1));
//     conn->SetConnectedCallback(std::bind(OnConnected, std::placeholders::_1));
//     conn->EnableInactiveRelease(10);//启动非活跃超时销毁
//     conn->Established();//就绪初始化
    
//     _conns.insert(std::make_pair(conn_id, conn));

// }


void NewConnection(int fd){
    conn_id++;
    // Channel*channel=new Channel(loop,newfd);
    PtrConnection conn(new Connection(loops->NextLoop(), conn_id, fd));
    conn->SetMessageCallback(std::bind(OnMessage, std::placeholders::_1, std::placeholders::_2));
    conn->SetSrvClosedCallback(std::bind(ConnectionDestroy, std::placeholders::_1));
    conn->SetConnectedCallback(std::bind(OnConnected, std::placeholders::_1));
    conn->EnableInactiveRelease(10);//启动非活跃超时销毁
    conn->Established();//就绪初始化
    
    _conns.insert(std::make_pair(conn_id, conn));
    DBG_LOG("new connection");

}

int main()
{
    loops=new LoopThreadPool(&base_loop);
    loops->SetThreadCount(2);
    loops->Create();
    Acceptor accept(&base_loop,8081);
    accept.SetAcceptCallback(std::bind(NewConnection,std::placeholders::_1));
    accept.Listen();
    base_loop.Start();
    // while(1){
    //     base_loop.Start();
    // }
    return 0;
}

// int main()
// {
//     EventLoop loop;
//     Socket lst_sock; 
//     lst_sock.CreateServer(8081);
//     Channel channel(&loop,lst_sock.Fd());
//     channel.SetReadCallback(std::bind(Acceptor,&loop,&channel));
//     // DBG_LOG("channel fd:%d",channel.Fd());
//     channel.EnableRead();
//     while(1){
//         loop.Start();
//     }
//     lst_sock.Close();
//     return 0;
// }
// void HandleClose(Channel* channel){
//     std::cout<<"close: "<<channel->Fd()<<std::endl;
//     DBG_LOG("close:%d ",channel->Fd());
//     channel->Remove();
//     close(channel->Fd());
//     delete channel;
// }

// void HandleRead(Channel* channel){
//     int fd=channel->Fd();
//     char buf[1024]={0};
//     int ret=recv(fd,buf,1023,0);
//     if(ret<=0){
//         return HandleClose(channel);
//     }
//     // std::cout<<buf<<std::endl;
//     DBG_LOG("%s",buf);
//     channel->EnableWrite();
// }

// void HandleWrite(Channel* channel){
//     int fd=channel->Fd();
//     const char*data="hello world!!";
//     int ret=send(fd,data,strlen(data),0);
//     if(ret<0){
//         return HandleClose(channel);
//     }
//     channel->DisableWrite(); 
// }


// void HandleError(Channel* channel){
//     return HandleClose(channel);
// }
// void HandleEvent(EventLoop* loop,Channel* channel,uint64_t timeid){
//     // std::cout<<"有了一个事件！！"<<std::endl;
//     loop->TimerRefresh(timeid);
// }


// void Acceptor(EventLoop* loop,Channel* lst_channel){
//     int fd=lst_channel->Fd();
//     int newfd=accept(fd,nullptr,nullptr);
//     // DBG_LOG("获得了一个fd：%d",fd);
//     if(newfd<0) return;
//     Channel*channel=new Channel(loop,newfd);
//     channel->SetReadCallback(std::bind(HandleRead,channel));
//     channel->SetWriteCallback(std::bind(HandleWrite,channel));
//     channel->SetCloseCallback(std::bind(HandleClose,channel));
//     channel->SetErrorCallback(std::bind(HandleError,channel));
//     channel->SetEventCallback(std::bind(HandleEvent,loop,channel,1));
//     channel->EnableRead();
// }

// int main()
// {
//     Socket list_sock;
//     list_sock.CreateServer(8081);
//     while(1){
//         int newfd=list_sock.Accept();
//         if(newfd<0){
//             continue;
//         }
//         Socket sock(newfd);
//         char buff[1024]={0};
//         int ret=sock.Recv(buff,1023);
//         if(ret<0){
//             sock.Close();
//             continue;
//         }
//         sock.Send(buff,ret);
//         sock.Close();
//     }
//     list_sock.Close();
//     return 0;
// }